package action.ManagerAction;

import com.alibaba.fastjson.JSON;
import com.opensymphony.xwork2.ActionSupport;
import common.Pagination;
import entity.CroAddress;
import entity.CroCustomer;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import service.CroAddressService;
import service.CroCustomerService;
import vo.CroCustomerVo;

import java.util.List;

/**
 * Created by xuweijie on 17-5-11.
 */
public class CustomerAction extends ActionSupport {

    @Autowired
    private CroCustomerService croCustomerService;
    @Autowired
    private CroAddressService croAddressService;

    private List<CroCustomer> croCustomers;
    private List<CroAddress> croAddresses;
    private CroCustomerVo croCustomerVo;
    private CroCustomerVo recroCustomerVo;
    private CroCustomer croCustomer;
    private String successResult;
    private String customerid;
    private String addressid;
    private String del_flag;
    private String freeze_flag;
    private int pageIndex;
    private int pageCount;
    private int pageSize = 10;
    private String jsonObject;

    /*
    * 分页展示用户信息
    * */
    @RequiresPermissions("user:select")
    public String  showdetail(){
        Pagination<CroCustomer> page;
        if(pageIndex==0){
            page=croCustomerService.findAllUser(1,pageSize);
        }else{
            page=croCustomerService.findAllUser(pageIndex,pageSize);
        }
        setPageCount((int) page.getPagesCount());
        setPageIndex((int) page.getCurIndex());
        setCroCustomers(page.getItems());
        return SUCCESS;
    }

    /*
    * 多属性搜索
    * */
    @RequiresPermissions("user:select")
    public String querylist(){
        Pagination<CroCustomer> page;
        if(pageIndex==0){
            page=croCustomerService.FindUserByProperty(croCustomerVo,1,pageSize);
        }else {
            page=croCustomerService.FindUserByProperty(croCustomerVo,pageIndex,pageSize);
        }
        setPageCount((int) page.getPagesCount());
        setPageIndex((int) page.getCurIndex());
        setRecroCustomerVo(croCustomerVo);
        jsonObject= JSON.toJSONString(recroCustomerVo);
        setCroCustomers(page.getItems());
        return SUCCESS;
    }

    /**
     * 删除顾客
     * */
    @RequiresPermissions("user:delete")
    public String deletecus(){
        if(croCustomerService.deleteCus(customerid,del_flag)){
            successResult="1";
        }else {
            successResult="2";
        }
        return SUCCESS;
    }

    /*
    * 冻结/解冻顾客
    * */
    @RequiresPermissions("user:delete")
    public String freezecus(){
        if(croCustomerService.freezeCus(customerid,freeze_flag)){
            if(freeze_flag.equals("0")){
                successResult="1";
            }else{
                successResult="2";
            }

        }else {
            if(freeze_flag.equals("0")){
                successResult="3";
            }else{
                successResult="4";
            }
        }
        return SUCCESS;
    }

    /*
    * 查看用户详细信息(分页)
    * */
    @RequiresPermissions("user:select")
    public String customerdetail(){
        Pagination<CroAddress> page;
        croCustomer=croCustomerService.FindCustomerById(customerid);
        if(pageIndex==0){
            page=croAddressService.FindAddressByCusId(croCustomer,1,pageSize);
        }else {
            page=croAddressService.FindAddressByCusId(croCustomer,pageIndex,pageSize);
        }
        setPageCount((int) page.getPagesCount());
        setPageIndex((int) page.getCurIndex());
        setCroAddresses(page.getItems());
        return "customerdetail";
    }

    /*
    * 根据地址id删除地址信息
    * */
    @RequiresPermissions("user:delete")
    public String deleteaddress(){
        if(croAddressService.DeleteAddressById(addressid,del_flag)){
            successResult="0";
        }else{
            successResult="1";
        }
        return "customerdetail";
    }

    public List<CroCustomer> getCroCustomers() {
        return croCustomers;
    }

    public void setCroCustomers(List<CroCustomer> croCustomers) {
        this.croCustomers = croCustomers;
    }

    public CroCustomerVo getCroCustomerVo() {
        return croCustomerVo;
    }

    public void setCroCustomerVo(CroCustomerVo croCustomerVo) {
        this.croCustomerVo = croCustomerVo;
    }

    public String getCustomerid() {
        return customerid;
    }

    public void setCustomerid(String customerid) {
        this.customerid = customerid;
    }

    public String getDel_flag() {
        return del_flag;
    }

    public void setDel_flag(String del_flag) {
        this.del_flag = del_flag;
    }

    public String getFreeze_flag() {
        return freeze_flag;
    }

    public void setFreeze_flag(String freeze_flag) {
        this.freeze_flag = freeze_flag;
    }

    public String getSuccessResult() {
        return successResult;
    }

    public void setSuccessResult(String successResult) {
        this.successResult = successResult;
    }

    public int getPageIndex() {
        return pageIndex;
    }

    public void setPageIndex(int pageIndex) {
        this.pageIndex = pageIndex;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public String getJsonObject() {
        return jsonObject;
    }

    public void setJsonObject(String jsonObject) {
        this.jsonObject = jsonObject;
    }

    public CroCustomerVo getRecroCustomerVo() {
        return recroCustomerVo;
    }

    public void setRecroCustomerVo(CroCustomerVo recroCustomerVo) {
        this.recroCustomerVo = recroCustomerVo;
    }

    public CroCustomer getCroCustomer() {
        return croCustomer;
    }

    public void setCroCustomer(CroCustomer croCustomer) {
        this.croCustomer = croCustomer;
    }

    public List<CroAddress> getCroAddresses() {
        return croAddresses;
    }

    public void setCroAddresses(List<CroAddress> croAddresses) {
        this.croAddresses = croAddresses;
    }

    public String getAddressid() {
        return addressid;
    }

    public void setAddressid(String addressid) {
        this.addressid = addressid;
    }
}
